<?php

namespace app\api\controller;

use app\api\model\ConfigInvite;
use app\api\model\User;
use think\response\Json;
use app\api\model\VolleyDesc as Balance;
use app\api\model\YaoqingRecords as Income;
use think\Db;

class Invite extends Common
{
    public function index(): Json
    {
        // return $this->pass([]);
        $data = [
            'invite_code' => $this->member['yqm'],
            'invite_count' => (new User)->where(['yqr' => $this->member['yqm']])->count(),
            'income_grade' => '',
            'income_grade_icon' => '',
            'topup_grade' => '',
            'topup_grade_icon' => '',
            'income' => 0,
            'topup' => 0
        ];


        $config = ConfigInvite::index($this->member);

        if ($config['income']) {
            $data['income_grade'] = $config['income']['name'];
            $data['income_grade_icon'] = changeUrl($config['income']['icon']);
        } else {
            $ConfigInviteModel = new ConfigInvite;
            $configInvite = $ConfigInviteModel->where(['types' => 1, 'min' => 0])->find();
            if ($configInvite) {
                $data['income_grade'] = $configInvite['name'];
                $data['income_grade_icon'] = changeUrl($configInvite['icon']);
            }
        }
        if ($config['topup']) {
            $data['topup_grade'] = $config['topup']['name'];
            $data['topup_grade_icon'] = changeUrl($config['topup']['icon']);
        } else {
            $ConfigInviteModel = new ConfigInvite;
            $configInvite = $ConfigInviteModel->where(['types' => 2, 'min' => 0])->find();
            if ($configInvite) {
                $data['topup_grade'] = $configInvite['name'];
                $data['topup_grade_icon'] = changeUrl($configInvite['icon']);
            }
        }
        $data['income_grade'] = '';
        $data['topup_grade'] = '';

        $config = ConfigInvite::zhiyao($this->member);

        $data = array_merge($data, $config);



        $reward = Db::name('user_invite_record')->where(['user_id'=>$this->member['id'], 'state'=>1, 'reward_get'=>0])->sum('reward');

        if ($reward <= 0) {
            $reward = '邀请好友有机会获得相应的金币奖励';
        } else {
            // if ($this->member['sex'] == '女') {
            //     $reward = '可领取 '.$reward.'积分';
            // } else {
                $reward = '可领取 '.$reward.'赠送金币';
            // }
        }

        $data['reward'] = $reward;

        return $this->pass($data);
    }

    public function roleMaleList(): Json
    {
        $page = $this->params['page'] ?? 1;
    
        $where = ['sex' => '男', 'role' => 1];
        
        $data = User::all(function($query) use ($page, $where) {
            $query->field('id, name, head_100 avatar, phone, wxcount')->where($where)->page($page, 10);
        });
    
        foreach ($data as $key => $item) {
            $data[$key]['avatar'] = changeUrl($item['avatar']);
        }
        
        return $this->pass($data);
    }

    public function reward()
    {
        // $this->member = db('user')->find(12);
        $records = Db::name('user_invite_record')->where(['user_id'=>$this->member['id'], 'state'=>1, 'reward_get'=>0])->column('reward','touserid');
        $sum = floatval(array_sum(array_values($records)));
        if ($sum <= 0) return $this->miss('暂无奖励可领取,快去邀请好友吧');

        Db::name('user_invite_record')->where(['user_id'=>$this->member['id'], 'state'=>1, 'reward_get'=>0])->update(['reward_get'=>1, 'reward_get_time'=>time()]);
        // if ($this->member['sex'] == '女') {
        //     foreach ($records as $key => $value) {
        //         Income::index($this->member['id'], $key, $value, '邀请奖励', 1);
        //     }
        //     $type = '积分';
        // } else {
            foreach ($records as $key => $value) {
                Balance::index($this->member['id'], $value, "邀请($key)", 1, 1);
            }
            $type = '赠送金币';
        // }

        return $this->pass('成功领取 '. $sum . $type);
    }
}
